/*
 * NieuwWachtwoordFrame.java
 *
 * Created on 22 april 2008, 13:40
 */

package be.hogent.iii.project.client.gui.login;

import be.hogent.iii.project.client.netwerk.Connectie;
import be.hogent.iii.project.common.bericht.login.NieuwWachtwoordBericht;
import com.thoughtworks.xstream.XStream;
import javax.swing.JOptionPane;

/**
 *
 * @author  Caribou
 */
public class NieuwWachtwoordFrame extends javax.swing.JFrame {
    
   private static NieuwWachtwoordFrame frame;
    
    /** Creates new form LoginFrame */
    private NieuwWachtwoordFrame() {
        initComponents();
        setLocationRelativeTo(null);
    }

    public static synchronized NieuwWachtwoordFrame getNieuwWachtwoordFrame() {
        if (frame == null) {
                frame = new NieuwWachtwoordFrame();
        }
        return frame;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        infoLbl = new javax.swing.JLabel();
        gebruikerLbl = new javax.swing.JLabel();
        wachtwoordLbl = new javax.swing.JLabel();
        bevestigWachtwoordLbl = new javax.swing.JLabel();
        bevestigWachtwoordPwd = new javax.swing.JPasswordField();
        wachtwoordPwd = new javax.swing.JPasswordField();
        gebruikerTxt = new javax.swing.JTextField();
        annulerenBrn = new javax.swing.JButton();
        nieuwWachtwoordBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nieuw wachtwoord");

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        infoLbl.setFont(new java.awt.Font("Tahoma", 3, 11));
        infoLbl.setForeground(new java.awt.Color(255, 153, 0));
        infoLbl.setText("Geef je gebruikersnaam in, en maak een nieuw wachtwoord. ");

        gebruikerLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        gebruikerLbl.setForeground(new java.awt.Color(255, 153, 0));
        gebruikerLbl.setText("Gebruikersnaam: ");

        wachtwoordLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        wachtwoordLbl.setForeground(new java.awt.Color(255, 153, 0));
        wachtwoordLbl.setText("Nieuw wachtwoord: ");

        bevestigWachtwoordLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        bevestigWachtwoordLbl.setForeground(new java.awt.Color(255, 153, 0));
        bevestigWachtwoordLbl.setText("Bevestig wachtwoord: ");

        bevestigWachtwoordPwd.setBackground(new java.awt.Color(102, 0, 0));
        bevestigWachtwoordPwd.setForeground(new java.awt.Color(204, 153, 0));

        wachtwoordPwd.setBackground(new java.awt.Color(102, 0, 0));
        wachtwoordPwd.setForeground(new java.awt.Color(204, 153, 0));

        gebruikerTxt.setBackground(new java.awt.Color(102, 0, 0));
        gebruikerTxt.setForeground(new java.awt.Color(204, 153, 0));

        annulerenBrn.setBackground(new java.awt.Color(102, 0, 0));
        annulerenBrn.setForeground(new java.awt.Color(204, 153, 0));
        annulerenBrn.setText("Annuleren");
        annulerenBrn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annulerenBrnActionPerformed(evt);
            }
        });

        nieuwWachtwoordBtn.setBackground(new java.awt.Color(102, 0, 0));
        nieuwWachtwoordBtn.setForeground(new java.awt.Color(204, 153, 0));
        nieuwWachtwoordBtn.setText("Maak nieuw wachtwoord");
        nieuwWachtwoordBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nieuwWachtwoordBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(infoLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(bevestigWachtwoordLbl)
                            .addComponent(wachtwoordLbl)
                            .addComponent(gebruikerLbl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(nieuwWachtwoordBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(annulerenBrn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(bevestigWachtwoordPwd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                            .addComponent(wachtwoordPwd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                            .addComponent(gebruikerTxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))
                        .addGap(28, 28, 28))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(infoLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gebruikerLbl)
                    .addComponent(gebruikerTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(wachtwoordLbl)
                    .addComponent(wachtwoordPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bevestigWachtwoordLbl)
                    .addComponent(bevestigWachtwoordPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(annulerenBrn)
                    .addComponent(nieuwWachtwoordBtn))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nieuwWachtwoordBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nieuwWachtwoordBtnActionPerformed
        if(wachtwoordengelijk(wachtwoordPwd.getPassword(), bevestigWachtwoordPwd.getPassword())){
            NieuwWachtwoordBericht bericht = new NieuwWachtwoordBericht(gebruikerTxt.getText(), wachtwoordPwd.getPassword());
            XStream xStream = new XStream();
            Connectie connectie = LoginFrame.getLoginFrame().getConnectie();
            connectie.stuurBericht(xStream.toXML(bericht));
            
            this.dispose();
        }
        else{
            JOptionPane.showMessageDialog(this,"De twee wachtwoorden zijn niet gelijk", "Fout", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_nieuwWachtwoordBtnActionPerformed

    private void annulerenBrnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annulerenBrnActionPerformed
        this.dispose(); 
}//GEN-LAST:event_annulerenBrnActionPerformed
    
    private boolean wachtwoordengelijk(char[] paswoord1, char[] paswoord2){
        boolean gelijk = true;
        if(paswoord1.length == paswoord2.length){
            int i = 0;
            while(i < paswoord1.length && gelijk){
                gelijk = (paswoord1[i]==paswoord2[i]);
                i++;
            }
        }
        else{
            gelijk = false;
        }
        return gelijk;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NieuwWachtwoordFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton annulerenBrn;
    private javax.swing.JLabel bevestigWachtwoordLbl;
    private javax.swing.JPasswordField bevestigWachtwoordPwd;
    private javax.swing.JLabel gebruikerLbl;
    private javax.swing.JTextField gebruikerTxt;
    private javax.swing.JLabel infoLbl;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton nieuwWachtwoordBtn;
    private javax.swing.JLabel wachtwoordLbl;
    private javax.swing.JPasswordField wachtwoordPwd;
    // End of variables declaration//GEN-END:variables
    
}
